<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Tools\Wechat;
use App\Tools\Fun;

class WeChatController extends Controller
{
    public function login(Request $request) {
        if (Fun::is_windows()) {
            $webUrl = "http://".$request->host().":5173/#/pages/index/index?openid=oNqg96oGnv6tj0OMJmli--J92ekY";    
            return redirect($webUrl);
        }
        
        $redirectUrl = "http://edu.younice.top/web/we-chat/callback";
        $oauthUrl = Wechat::getOauth()->scopes(['snsapi_userinfo'])->redirect($redirectUrl);
        return redirect($oauthUrl);
    }
    public function callback(Request $request) {
        $code = $request->input('code');
        $info = Wechat::getOauth()->userFromCode($code);
        $id = $info->id;
        if (Fun::is_windows()) {
            $redirectUrl = "http://192.168.0.107:5173/#/pages/index/index?openid=" . $id;    
        } else {
            $redirectUrl = "http://edu.younice.top/mini#/pages/index/index?openid=" . $id;    
        }
        
        return redirect($redirectUrl);
    }
}
